<template>
  <ContainerContent :loading="loading" :pagination="pagination" @run="run">
    <template #header>
      <a-button type="primary" @click="open">
        <i-icons-btn-add />
        添加DNS API
      </a-button>
      <CloudFilterBar
        v-model="queryParams"
        :run="run"
        :schemes="filterSchemes"></CloudFilterBar>
    </template>
    <a-table
      ref="tableRef"
      v-bind="commonTableProps"
      :columns="columns"
      :data="data"
      @sorter-change="onSorterChange">
      <!-- <template #secret="{ record }">
        <ViewSecret :record="record" />
      </template> -->
      <template #action="{ record }">
        <a-link :hoverable="false" @click="open(record)"> 编辑 </a-link>
        <a-link
          status="danger"
          :hoverable="false"
          @click="handDelete(record.id)">
          删除
        </a-link>
      </template>
    </a-table>
    <AddDns v-model:visible="visible" :record="dnsRecord" @reload="run">
      ></AddDns
    >
  </ContainerContent>
</template>
<script lang="ts" setup>
import {
  deleteApiResolveDnsApiApi,
  getApiResolveDnsApisApi
} from '@/apis/modules/resolve/dns'
import ContainerContent from '@/components/ContentContainer.vue'
import type { CloudFilterBarState } from '@/components/filter-bar/types'
import { useListRequest } from '@/hooks/useListRequest'
import { commonTableProps } from '@/utils/table'
import { Message } from '@arco-design/web-vue'
import useTableColumn from './column'
import AddDns from './components/AddDns.vue'
// import ViewSecret from './components/ViewSecret.vue'

const queryParams = ref<CloudFilterBarState>({})
const { visible, open, data: dnsRecord } = usePopup()

const { columns, filterSchemes } = useTableColumn()
const { data, loading, run, pagination } = useListRequest(
  getApiResolveDnsApisApi,
  {
    params: queryParams,

    immediate: true
  }
)

const { createSortChange } = useTable(run, data)

const onSorterChange = createSortChange(queryParams)

const handDelete = (id: string) => {
  CloudModal.error(
    {
      content: '该操作会删除此 DNS 配置，确认删除 ？',
      okText: '删除'
    },
    () =>
      deleteApiResolveDnsApiApi({ id }).then(() => {
        Message.success('删除成功')
        run()
      })
  )
}
</script>
